package com.jade.editor.domain.ani;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.swt.graphics.Point;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AnisotropyModel {
	
	private Point _size = null;
	private ArrayList<Cortege> _clist = new ArrayList<Cortege>();
	private ArrayList<Point> _plist = new ArrayList<Point>();
	
	private String _path = null;
	
	public AnisotropyModel(String path){
		_path = path;
		load();
	}
	
	public String getAniPath(){
		return _path;
	}
	
	public void setCortegeList(ArrayList<Cortege> list){
		_clist = list;
	}
	
	public void setPointList(ArrayList<Point> list){
		_plist = list;
	}
	
	public void setSize(Point size){
		_size = size;
	}
	
	public ArrayList<Cortege> getCortegeList(){
		return _clist;
	}
	
	public ArrayList<Point> getPointList(){
		return _plist;
	}
	
	public Point getSize(){
		return _size;
	}
	
	public void terminateData(){
		_size = null;
		_clist.clear();
		_plist.clear();
	}
	
	
	public void load(){
		File file = new File(_path);
		
		if(file.exists()){
			if(file.length() > 0){
				

				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder;
				
				try{
					
					dBuilder = dbFactory.newDocumentBuilder();
					Document doc = dBuilder.parse(file);
					doc.getDocumentElement().normalize();
					
					if(doc.hasChildNodes()){
						
						if(doc.getDocumentElement().getNodeName().toLowerCase().indexOf("anisotropy") == 0){
							
							//--SIZE
							if(doc.getDocumentElement().hasAttributes()){
								
								String xString = doc.getDocumentElement().getAttributeNode("x").getValue();
								String yString = doc.getDocumentElement().getAttributeNode("y").getValue();
								
								int x = Integer.parseInt(xString);
								int y = Integer.parseInt(yString);
								
								_size = new Point(x, y);
								
							}
							
							NodeList rootNodeList = doc.getDocumentElement().getChildNodes();
							
							for(int i = 0; i < rootNodeList.getLength(); i++){
								
								//---Cortege
								if(rootNodeList.item(i).getNodeName().toLowerCase().equals("cortege")){
									if(rootNodeList.item(i).hasChildNodes()){
										NodeList cortegeNodeList = rootNodeList.item(i).getChildNodes();
										ArrayList<Point> pcList = new ArrayList<Point>();
										
										for(int j = 0; j < cortegeNodeList.getLength(); j++){
											if(cortegeNodeList.item(j).getNodeName().toLowerCase().equals("point")){
												String xString = ((Element) cortegeNodeList.item(j)).getAttribute("x");
												String yString = ((Element) cortegeNodeList.item(j)).getAttribute("y");
												
												int x = Integer.parseInt(xString);
												int y = Integer.parseInt(yString);
												
												pcList.add(new Point(x,y));
												
											}
										}
										
										_clist.add(new Cortege(pcList));
										
									}
								}
								
								//---Point
								if(rootNodeList.item(i).getNodeName().toLowerCase().equals("point")){
									String xString = ((Element) rootNodeList.item(i)).getAttribute("x");
									String yString = ((Element) rootNodeList.item(i)).getAttribute("y");
									
									int x = Integer.parseInt(xString);
									int y = Integer.parseInt(yString);
									
									_plist.add(new Point(x,y));
								}
								
							}
							
						}
						
					}
					
					System.out.println("Ani Load OK");
					
				}
				catch(Exception e){
					e.printStackTrace();
				}
				
				
			}
		}
	}

}
